package com.example.hp.qingyi.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import com.example.hp.qingyi.R;
import com.example.hp.qingyi.base.BaseFragment;
import com.example.hp.qingyi.company.fragment.CompanyFragment;
import com.example.hp.qingyi.compass.fragment.fragment.CompassFragment;
import com.example.hp.qingyi.home.fragment.HomeFragment;
import com.example.hp.qingyi.scenic.fragment.ScenicFragment;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomeActivity extends FragmentActivity {
    @BindView(R.id.fl)
    FrameLayout fl;
    @BindView(R.id.rg_main)
    RadioGroup rgMain;

    private int flag;
    private ArrayList<BaseFragment> fragmentList;//存放Fragment的列表
    private int position;//fragmentList的下标
    private BaseFragment tempFragment;//上次显示的Fragment
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
    //初始化Fragment列表
        initFragment();
        //切换Fragment的监听方法
        initListener();
    }
    /**
     * 按顺序添加Fragment
     * */
    private void initFragment(){
        fragmentList=new ArrayList<>();
        fragmentList.add(new HomeFragment());
        fragmentList.add(new CompanyFragment());
        fragmentList.add(new CompassFragment());
        fragmentList.add(new ScenicFragment());
    }
    /**
     * 切换Fragment的监听方法
     * */
    private void initListener(){
        rgMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_select:
                        position=0;
                        break;
                    case R.id.rb_study:
                        position=1;
                        break;
                    case R.id.rb_master:
                        position=2;
                        break;
                    case R.id.rb_account:
                        position=3;
                        break;
                    default:break;
                }
                //按下标获取Fragment
                BaseFragment fragment = getFragment(position);
                switchFragment(tempFragment,fragment);
            }
        });
        rgMain.check(R.id.rb_select);
    }
    /**
     * 按下标获取Fragment的方法
     * */
    private BaseFragment getFragment(int position){
        if(fragmentList!=null&&fragmentList.size()>0){
            return fragmentList.get(position);
        }
        return null;
    }
    /**
     * 切换Fragment的方法
     * */
    private void switchFragment(BaseFragment fromfragment,BaseFragment nextFragment) {
        if (tempFragment != nextFragment) {
            tempFragment = nextFragment;
            if (nextFragment != null) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                //判断nextFragment是否添加
                if (!nextFragment.isAdded()) {
                    //隐藏掉之前的Fragment
                    if (fromfragment != null) {
                        transaction.hide(fromfragment);
                    }
                    //添加现在的Fragment
                    transaction.add(R.id.fl, nextFragment).commit();
                } else {
                    //隐藏掉之前的Fragment
                    if (fromfragment != null) {
                        transaction.hide(fromfragment);
                    }
                    //显示现在的Fragment
                    transaction.show(nextFragment).commit();
                }
            }
        }
    }

}
